#!/bin/bash

echo "+++++++++++++++++++++++ $(cd `dirname $0`; pwd)/$(basename $0) +++++++++++++++++++++++++"

# 添加subversion仓库地址
v_d_current=$(cd `dirname $0`; pwd)
\cp ${v_d_current}/*.repo "/etc/yum.repos.d/"

# 卸载已安装的subversion
yum -y clean all
yum -y remove subversion*
# 安装仓库指定版本的subversion
yum -y install subversion  mod_dav_svn

# 配置
v_f_settings=${v_d_current}/settings
# 检查配置文件是否存在
if [ ! -f ${v_f_settings} ]; then
    echo "settings file does not exist : $v_f_settings"
    exit -1
fi
# 加载配置信息
source ${g_f_util_properties} "load" ${v_f_settings}
# svn仓库目录
if [ ! -d ${svnrepo} ]; then
    echo "create svn repo : $svnrepo"
    mkdir -p ${svnrepo}
fi
# svn配置目录
if [ ! -d ${svnconf} ]; then
    echo "create svn conf : $svnconf"
    mkdir -p ${svnconf}
fi
# 需要创建的仓库
if [ -n "$repos" ]; then
    v_repos=(${repos//,/ })
    for repo in ${v_repos}
    do
        v_d_repo=${svnrepo}/${repo}
        if [ -d ${v_d_repo}  ]; then
            echo "repo already exists : $v_d_repo"
            continue
        fi
        svnadmin create ${v_d_repo}
        chown -R apache.apache ${v_d_repo}
    done
fi

# subversion认证配置 svn-auth-conf
v_f_auth=${v_d_current}/svn-auth-conf
if [ -f ${v_f_auth} ]; then
    \cp ${v_f_auth} ${svnconf}
fi
# subversion鉴权配置 svn-acl-conf
v_f_acl=${v_d_current}/svn-acl-conf
if [ -f ${v_f_acl} ]; then
    \cp ${v_f_acl} ${svnconf}
fi
# subversion http配置
v_f_http=${v_d_current}/subversion.conf
if [ -f ${v_f_http} ]; then
    v_f_svnconf="/etc/httpd/conf.d/subversion.conf"
    \cp ${v_f_svnconf} ${v_f_svnconf}.backup
    ${g_f_util_replace} -s ${v_f_http} -t ${v_f_svnconf} svnrepo=${svnrepo}  svnconf=${svnconf}
fi
# 重启httpd
service httpd restart
exit -1